package logic

import (
	"context"
	"errors"
	"renting-demo/model/mysql"

	"renting-demo/goods_srv/goods"
	"renting-demo/goods_srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetProductLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetProductLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetProductLogic {
	return &GetProductLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 分页，搜索商品列表
func (l *GetProductLogic) GetProduct(in *goods.GetProductReq) (*goods.GetProductResponse, error) {
	// todo: add your logic here and delete this line
	search, res := mysql.PageOrSearch(in.Page, in.Search)
	if len(res) == 0 {
		return nil, errors.New("服务器异常")
	}
	var product []*goods.Product
	for _, v := range res {
		product = append(product, &goods.Product{
			GoodsID:    int64(v.ID),
			GoodsName:  v.GoodsName,
			GoodsStock: v.GoodsStock,
			GoodsPrice: float32(v.GoodsPrice),
			GoodsImg:   v.GoodsImages,
			Status:     v.Status,
		})
	}

	return &goods.GetProductResponse{
		Goods:  product,
		Search: search,
	}, nil
}
